<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
        }
        form img{
            position: absolute;
            top: 50%;
            right: 10px;
            width: 22px;
            height: 22px;
        }
        .img-td{
            position: relative;
        }
    </style>
</head>
<body>
    <form action="#" method="post">
        <table>
            <tr>
                <td>密码</td>
                <td class="img-td">
                    <input type="password" name="pwd" id="pwd">
                    <img id="eye" src="../images/close.png" alt="">
                </td>
            </tr>
        </table>
    </form>

    <script>
        //1.找到元素对象
        var img=document.querySelector('#eye');
        var txt=document.querySelector('#pwd');
        var flag=1;//闭眼
        //2.绑定click事件处理程序
        img.onclick=function(){
            //闭眼变开眼  开眼变闭眼
    
            if(flag==1){
                img.src='../images/open.png';
                //文本框从密码文本框变为一般文本框
                txt.type='text';
                flag=0;
            }else{
                img.src='../images/close.png';
                //文本框从一般文本框变为密码文本框
                txt.type='password';
                flag=1;
            }
        }
    </script>
</body>
</html>